package com.c.ctgproject.ui.collect

import android.content.Intent
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityCarWeighingChildBinding
import com.c.ctgproject.entity.guang.CarInfoEntityV2
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.fragment.review.car.AgreeCarViewModel
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.google.gson.Gson
import com.lwkandroid.imagepicker.ImagePicker
import com.lwkandroid.imagepicker.data.ImageBean
import com.lwkandroid.widget.ninegridview.NineGridBean
import org.greenrobot.eventbus.EventBus
import java.io.File

/**
 *作者:LiBW
 *创建日期: 2021/9/9
 *描述: 车辆过磅 页面
 */
class CarWeighingChildActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityCarWeighingChildBinding.inflate(layoutInflater) }

    override fun getLayout() = mBinding.root
    private val imageResultCode = 100
    private val imageShowList = ArrayList<String>()
    private val mCarId by lazy { intent.getIntExtra(MyConstants.CAR_ID, -1) }
    private val mListPosition by lazy { intent.getIntExtra(MyConstants.POSITION, -1) }
    private val mCarIdList by lazy {
        intent.getStringExtra(MyConstants.CAR_ID_LIST)
    }

    override fun event() {
        if(mCarId!=-1){
            ApiMethods.apiSubscribe(ApiMethods.retrofit.carDetailHasImage(mCarId), MyObserverHandlerLoading(this, loading) {
                MyLog.d("车辆详情回调:$it")
                val entity = getGson().fromJson(it, CarInfoEntityV2::class.java).data
                entity?.let {
                    mBinding.poundNoET.setText(entity.poundNo.handlerNull())
                    mBinding.poundWeightET.setText(entity.poundWeight.handlerNull())
                    mBinding.purchasePoundNoET.setText(entity.purchasePoundNo)
                    mBinding.purchasePoundWeightET.setText(entity.purchaseWeight.handlerNull())
                    entity.poundPicture?.let {
                        val resultList: MutableList<NineGridBean> = ArrayList()
                        val nineGirdData = NineGridBean(it)
                        imageShowList.add(it)
                        resultList.add(nineGirdData)
                        mBinding.mPhotoNGV.addDataList(resultList)
                    }

                }
            })
        }
        mBinding.mPhotoNGV.init(this, imageShowList, 1, imageResultCode)
    }

    override fun click() {

        mBinding.mConfirmBTN.setOnClickListener {

            if(imageShowList.isEmpty()){
                onHttp()
            }else{
                onHttp(imageShowList[0])
            }
//            if (imageShowList.isEmpty()) {
//                onHttp()
//            } else {
//                val file = File(imageShowList[0])
//                //图片压缩
////                val newFile: File = CompressHelper.getDefault(this).compressToFile(file)
////                val bitmap = BitmapFactory.decodeFile(newFile.absolutePath)
////                val bitmaResult = ImageShuiYinUtil.createWaterMaskRightBottom(this, bitmap, "广汽内场管理系统", 10, 10)
////                val  ff=saveBitmapFile(this,bitmaResult,1)
////                val ff=ImageUtil.imageWaterMark(this,file,1)
//                val ff: File = WaterRemark.handlerWaterRemark2File(this, file, "1")
//
//
//                upDataFile(arrayListOf(ff)) {
//                    if (it != null && it.isNotEmpty()) {
//                        onHttp(it[0].fileUrl.handlerNull())
//                    } else {
//                        showToast("图片上传异常请稍后再试")
//                    }
//
//                }
//            }
        }
    }

    private fun onHttp(imageUrl: String? = null) {
        val poundNo = mBinding.poundNoET.text.handlerNull()
        val poundWeight = mBinding.poundWeightET.text.handlerNull()
        val purchasePoundNo = mBinding.purchasePoundNoET.text.handlerNull()
        val purchaseWeight = mBinding.purchasePoundWeightET.text.handlerNull()
        when {
            poundNo.isEmpty() -> showToast("请输入磅单号")
            poundWeight.isEmpty() -> showToast("请输入磅单重量")
            else -> {
                val map = HashMap<String, Any>()
                if (mCarId != -1) {
                    map["carId"] = mCarId
                }
                map["poundNo"] = poundNo
                map["poundWeight"] = poundWeight
                map["carIdList"] = Gson().fromJson(mCarIdList, ArrayList<Int>()::class.java)
                if (purchasePoundNo.isNotEmpty()) map["purchasePoundNo"] = purchasePoundNo
                if (purchaseWeight.isNotEmpty()) map["purchaseWeight"] = purchaseWeight
                if (imageUrl != null && imageUrl.isNotEmpty()) {
                    map["poundPicture"] = imageUrl
                }

                ApiMethods.apiSubscribe(ApiMethods.retrofit.carPound(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                    MyLog.d("提交回调:$it")
                    showToast("提交成功")
                    //EventBus.getDefault().post(PositionEvent(mListPosition))
                    setResult(1)
                    finish()
                })
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let {
            when (requestCode) {
                //图片回调
                imageResultCode -> {
                    val list: List<ImageBean>? = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA)
                    list?.let {
                        if(list.size>0){
                            val file = File(list[0].imagePath)
                            //图片压缩
//                val newFile: File = CompressHelper.getDefault(this).compressToFile(file)
//                val bitmap = BitmapFactory.decodeFile(newFile.absolutePath)
//                val bitmaResult = ImageShuiYinUtil.createWaterMaskRightBottom(this, bitmap, "广汽内场管理系统", 10, 10)
//                val  ff=saveBitmapFile(this,bitmaResult,1)
//                val ff=ImageUtil.imageWaterMark(this,file,1)
                            val ff: File = WaterRemark.handlerWaterRemark2File(this, file, "1")


                            upDataFile(arrayListOf(ff)) {
                                if (it != null && it.isNotEmpty()) {
                                    //onHttp(it[0].fileUrl.handlerNull())
                                    val resultList: MutableList<NineGridBean> = ArrayList()
                                    it[0].fileUrl?.let {
                                        val nineGirdData = NineGridBean(it)
                                        imageShowList.add(it)
                                        resultList.add(nineGirdData)
                                        mBinding.mPhotoNGV.addDataList(resultList)
                                    }

                                } else {
                                    showToast("图片上传异常请稍后再试")
                                }


                            }

                        }
                    }


                }
                else -> {

                }
            }
        }
    }
}